home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLINC.PAK / RICHEDIT.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  18KB  |  634 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.16  $
  6. //
  7. // Definition of class TRichEdit.
  8. //----------------------------------------------------------------------------
  9. #if !defined(OWL_RICHEDIT_H)
  10. #define OWL_RICHEDIT_H
  11.  
  12. #if !defined(BI_PLAT_WIN32)
  13. # error OWL: The Rich Edit encapsulation can only be used for WIN32 targets
  14. #endif
  15.  
  16. #if !defined(OWL_EDITFILE_H)
  17. # include <owl/editfile.h>
  18. #endif
  19. #if !defined(OWL_COMMCTRL_H)
  20. # include <owl/commctrl.h>
  21. #endif
  22. #if !defined(__RICHEDIT_H)
  23. # include <richedit.h>
  24. #endif
  25. #if !defined(__RICHOLE_H)
  26. # include <richole.h>
  27. #endif
  28.  
  29. #if defined(BI_NAMESPACE)
  30. namespace OWL {
  31. #endif
  32.  
  33. //
  34. // Macro representing all CHARFORMAT flags
  35. //
  36. #if !defined(CFM_ALL)
  37. #define CFM_ALL   (CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT |\
  38.                    CFM_PROTECTED | CFM_SIZE | CFM_COLOR | CFM_FACE | \
  39.                    CFM_OFFSET | CFM_CHARSET)
  40. #endif
  41.  
  42. //
  43. // Macro representing all the PARAFORMAT flags
  44. //
  45. #if !defined(PFM_ALL)
  46. #define PFM_ALL (PFM_ALIGNMENT|PFM_NUMBERING|PFM_OFFSET|PFM_OFFSETINDENT|\
  47.                  PFM_RIGHTINDENT|PFM_STARTINDENT|PFM_TABSTOPS)
  48. #endif
  49.  
  50. //
  51. // Forward References
  52. //
  53. class _OWLCLASS TRichEdit;
  54.  
  55.  
  56. // Generic definitions/compiler options (eg. alignment) preceeding the 
  57. // definition of classes
  58. #include <services/preclass.h>
  59.  
  60. //
  61. // class TCharFormat
  62. // ~~~~~ ~~~~~~~~~~~
  63. // TCharFormat encapsulates the CHARFORMAT structure which contains
  64. // information about character formatting in a rich edit control.
  65. //
  66. class _OWLCLASS TCharFormat : public CHARFORMAT {
  67.   public:
  68.     TCharFormat(ulong mask = 0);
  69.     TCharFormat(const TRichEdit&, bool selection = true,
  70.                 ulong mask = CFM_ALL);
  71.  
  72.     void    ToggleEffectsBit(ulong flag);
  73.     void    ToggleMaskBit(ulong flag);
  74.  
  75.     void    EnableBold(bool = true);
  76.     void    EnableItalic(bool = true);
  77.     void    EnableUnderline(bool = true);
  78.     void    EnableStrikeOut(bool = true);
  79.     void    EnableProtected(bool = true);
  80.  
  81.     void    SetHeight(long);
  82.     void    SetOffset(long);
  83.     void    SetTextColor(const TColor& = TColor::None);
  84.     void    SetCharSet(uint8);
  85.     void    SetPitchAndFamily(uint8);
  86.     void    SetFaceName(const char far*);
  87.  
  88.     TColor  GetTextColor() const;
  89.  
  90.     void    SetFontInfo(LOGFONT& lf) const;
  91.     void    GetFontInfo(const LOGFONT& lf);
  92. };
  93.  
  94. //
  95. // class TCharRange
  96. // ~~~~~ ~~~~~~~~~~
  97. // TCharRange encapsulates the CHARRANGE structure which specifies a range
  98. // of characters in a rich edit control.
  99. //
  100. class _OWLCLASS TCharRange : public CHARRANGE {
  101.   public:
  102.     TCharRange(long min = 0, long max = -1);
  103. };
  104.  
  105. //
  106. // class TParaFormat
  107. // ~~~~~ ~~~~~~~~~~~
  108. // TParaFormat encapsulates the PARAFORMAT structure which contains
  109. // information about paragraph formatting attributes of a rich edit control.
  110. //
  111. class _OWLCLASS TParaFormat : public PARAFORMAT {
  112.   public:
  113.     TParaFormat(ulong mask = 0);
  114.     TParaFormat(const TRichEdit&, ulong mask = CFM_ALL);
  115.  
  116.     void  ToggleMaskBit(ulong flag);
  117.  
  118.     void  SetNumbering(uint16);
  119.     void  SetStartIndent(long, bool relative = false);
  120.     void  SetRightIndent(long);
  121.     void  SetOffset(long);
  122.     void  SetAlignment(uint16);
  123.     void  SetTabCount(short, long*);
  124. };
  125.  
  126. //
  127. // class TFormatRange
  128. // ~~~~~ ~~~~~~~~~~~~
  129. // TFormatRange encapsulates the FORMATRANGE structure which contains
  130. // information that a rich edit control uses to format its output for a
  131. // particular device.
  132. //
  133. class _OWLCLASS TFormatRange : public FORMATRANGE {
  134.   public:
  135.     TFormatRange();
  136.     TFormatRange(HDC renderDC, HDC targetDC,
  137.                  const TRect& renderArea, const TRect& entireArea,
  138.                  const TCharRange& txtRange);
  139.  
  140.     TFormatRange(HDC renderDC, HDC targetDC,
  141.                  const TRect& renderArea, const TRect& entireArea,
  142.                  long firstChar, long lastChar);
  143.  
  144.     void  SetRenderDC(HDC);
  145.     void  SetTargetDC(HDC);
  146.     void  SetRenderRect(const TRect&);
  147.     void  SetPageRect(const TRect&);
  148.     void  SetRange(const TCharRange&);
  149.     void  SetRange(long, long);
  150. };
  151.  
  152. //
  153. // class TEditStream
  154. // ~~~~~ ~~~~~~~~~~~
  155. // TEditStream encapsulates the EDITSTREAM structure which contains
  156. // information about a data stream used with a rich edit control.
  157. //
  158. class _OWLCLASS TEditStream : public EDITSTREAM {
  159.   public:
  160.     TEditStream();
  161.     TEditStream(ulong cookie, EDITSTREAMCALLBACK);
  162. };
  163.  
  164. //
  165. // class TTextRange
  166. // ~~~~~ ~~~~~~~~~~
  167. // TTextRange encapsulates the TEXTRANGE structure which contains information
  168. // about a range of text in a rich edit control.
  169. //
  170. class _OWLCLASS TTextRange : public TEXTRANGE {
  171.   public:
  172.     TTextRange();
  173.     TTextRange(const TCharRange&, char far* buffer);
  174.     TTextRange(long beg, long end, char far* buffer);
  175. };
  176.  
  177. //
  178. // class TFindText
  179. // ~~~~~ ~~~~~~~~~
  180. // TFindText encapsulates the FINDTEXT structure which contains information
  181. // about text to search for in a rich edit control.
  182. //
  183. class _OWLCLASS TFindText : public FINDTEXT {
  184.   public:
  185.     TFindText(const TCharRange& range, const char far* text);
  186.     TFindText(long beg, long end, const char far* text);
  187. };
  188.  
  189. //----------------------------------------------------------------------------
  190.  
  191. //
  192. // class TRichEditModule
  193. // ~~~~~ ~~~~~~~~~~~~~~~
  194. // Wrapper object which loads the DLL providing the implementation of 
  195. // the RichEdit control
  196. //
  197. class _OWLCLASS TRichEditModule : public TModule {
  198.   public:
  199.     TRichEditModule();
  200. };
  201.  
  202. //
  203. // class TRichEditDll
  204. // ~~~~~~~~~~~~~~~~~~
  205. // TRichEditDll is a simple object which takes advantages of OWL's 
  206. // TDllLoader to ensure that only one copy of an object [theoretically
  207. // representing a DLL] is created.
  208. // TRichEditDll provides the 'IsAvailable' method which returns true if the
  209. // DLL is available and loaded. TRichEditDll is used internally by OWL's
  210. // TRichEdit class to ensure that the DLL is available and loaded. You may
  211. // consider using TRichEditDll if your application creates rich edit controls
  212. // without instantiating a TRichEdit object. (For example, if the control
  213. // is part of a dialog resource). The following snippet illustrates:
  214. //
  215. //    if (!TRichEditDll::IsAvailable()) {
  216. //      Error("Unable to load Rich Edit DLL");
  217. //    }
  218. //
  219. typedef TDllLoader<TRichEditModule> TRichEditDll;
  220.  
  221. #if defined(BI_IMPEXP_TEMPLATES)
  222. # if defined(_OWLDLL) || defined(BI_APP_DLL)
  223.   //
  224.   // Export template of TDllLoader<TRichEditModule> when building ObjectWindows
  225.   // DLL and provide import declaration of DLL instance for users of the class.
  226.   //
  227.   template class _OWLCLASS TDllLoader<TRichEditModule>;
  228. # endif   
  229. #endif   
  230.  
  231.  
  232. //
  233. // class TRichEdit
  234. // ~~~~~ ~~~~~~~~~
  235. // TRichEdit encapsulates a rich edit control, a window in which a user can
  236. // enter, edit and format text.
  237. //
  238. class _OWLCLASS TRichEdit : public TEditFile {
  239.   public:
  240.     TRichEdit(TWindow*        parent,
  241.               int             id,
  242.               const char far* text,
  243.               int x, int y, int w, int h,
  244.               const char far* fileName = 0,
  245.               TModule*        module = 0);
  246.     TRichEdit(TWindow*   parent,
  247.               int        resourceId,
  248.               TModule*   module = 0);
  249.  
  250.     // Generic Formatting Methods
  251.     //
  252.     ulong   GetCharFormat(TCharFormat far&, bool selection = false) const;
  253.     ulong   GetParaFormat(TParaFormat far&) const;
  254.     bool    SetCharFormat(const TCharFormat far&, uint flags= SCF_SELECTION);
  255.     bool    SetParaFormat(const TParaFormat far&);
  256.     TColor  SetBkgndColor(const TColor& = TColor::None);
  257.  
  258.     // Enumeration of flags returned when querrying a RichEdit about
  259.     // a particular character attribute for a chunk of selected text.
  260.     //
  261.     enum TFmtStatus {
  262.       No,               // The whole selection has the attribute
  263.       Yes,              // The attribute is absent from the selection
  264.       Partly            // Part of the selection has the attribute
  265.     };
  266.  
  267.     // More specialized character formatting or querrying methods
  268.     //
  269.     uint    HasCharAttribute(ulong mask, ulong effects);
  270.     bool    ToggleCharAttribute(ulong mask, ulong effects);
  271.     bool    ChangeCharPointSize(int pointSizeDelta);
  272.  
  273.  
  274.     // Selection and Hit Testing
  275.     //
  276.     bool    HasSelection() const;
  277.     void    GetSelection(uint& startPos, uint& endPos) const;
  278.     void    GetSelRange(TCharRange far&) const;
  279.     bool    SetSelection(uint startPos, uint endPos);
  280.     int     SetSelRange(const TCharRange far&);
  281.     void    HideSelection(bool hide, bool changeStyle);
  282.     ulong   GetSelectionType() const;
  283.  
  284.     // Text Operations
  285.     //
  286.     int     GetTextRange(TTextRange far&) const;
  287.     int     GetTextRange(const TCharRange far&, char far* buffer) const;
  288.     virtual void GetSubText(char far* str, uint startPos, uint endPos) const;
  289.     int     GetSelectedText(char far* buffer) const;
  290.     void    LimitText(long max);        
  291.     int     FindText(uint flags, const TFindText far&);
  292.     int     FindText(uint flags, const TCharRange far&, const char far* text);
  293.     int     Search(uint startPos, const char far* text,
  294.                    bool caseSensitive=false, bool wholeWord=false,
  295.                    bool up=false);
  296.  
  297.     // Word and Line Breaks
  298.     //
  299.     int     FindWordBreak(uint code, long start);
  300.     int     GetLineFromPos(uint charPos) const;       // EM_EXLINEFROMCHAR
  301.  
  302.     // Lines and Scrolling
  303.     //
  304.  
  305.     // Editing Operations
  306.     //
  307.     bool    CanPaste(uint format) const;
  308.     void    Paste();  // {HandleMessage(WM_PASTE);}
  309.     void    PasteSpecial(uint format);
  310.  
  311.     // Streams
  312.     //
  313.     ulong   StreamIn(uint format, TEditStream far&);
  314.     ulong   StreamOut(uint format, TEditStream far&);
  315.  
  316.     // Printing
  317.     //
  318.     bool    DisplayBand(TRect far&);
  319.     int     FormatRange(const TFormatRange far& range, bool render = true);
  320.     int     FormatRange();
  321.     bool    SetTargetDevice(HDC, int lineWidth);
  322.  
  323.     // Bottomless Rich Edit Controls
  324.     //
  325.     void    RequestResize();
  326.  
  327.     // OLE Interfaces
  328.     //
  329.     bool    GetOleInterface(IRichEditOle far* &) const;
  330.     bool    SetOleInterface(IRichEditOleCallback far*);
  331.  
  332.     // Querry/Set control's data format
  333.     //
  334.     uint    GetFormat() const;
  335.     void    SetFormat(uint fmt);
  336.     bool    IsRTF() const;
  337.  
  338.     // Misc
  339.     //
  340.     ulong   GetEventMask() const;
  341.     ulong   SetEventMask(ulong msk);
  342.  
  343.     // Override to use RichEdit streaming capabilities
  344.     //
  345.     uint    Transfer(void* buffer, TTransferDirection direction);
  346.  
  347.     // Override TEditFile's I/O
  348.     //
  349.     bool    Read(const char far* fileName=0);
  350.     bool    Write(const char far* fileName=0);
  351.  
  352.     bool    ReadFromStream(istream&, uint format = SF_RTF);
  353.     bool    WriteToStream(ostream&, uint format = SF_RTF);
  354.  
  355.   protected:
  356.  
  357.     // Data format of control
  358.     //
  359.     uint    Format;
  360.  
  361.     // Command response functions
  362.     //
  363.     void    CmEditCut();       // CM_EDITCUT
  364.     void    CmEditCopy();      // CM_EDITCOPY
  365.     void    CmEditPaste();     // CM_EDITPASTE
  366.     void    CmEditDelete();    // CM_EDITDELETE
  367.     void    CmEditClear();     // CM_EDITCLEAR
  368.     void    CmEditUndo();      // CM_EDITUNDO
  369.  
  370.     // Command enabler functions
  371.     //
  372.     void    CeHasSelect(TCommandEnabler& commandHandler);
  373.     void    CeEditPaste(TCommandEnabler& commandHandler);
  374.     void    CeEditClear(TCommandEnabler& commandHandler);
  375.     void    CeEditUndo(TCommandEnabler& commandHandler);
  376.  
  377.     // Child id notification handled at the child
  378.     //
  379.     void    ENErrSpace();  // EN_ERRSPACE
  380.  
  381.     // Override TWindow virtual member functions
  382.     //
  383.     char far* GetClassName();
  384.     void      SetupWindow();
  385.  
  386.     // Event handlers to override TEdit's handlers
  387.     //
  388.     void         EvChar(uint key, uint repeatCount, uint flags);
  389.     void         EvKeyDown(uint key, uint repeatCount, uint flags);
  390.     uint         EvGetDlgCode(MSG far*);
  391.     void         EvSetFocus(HWND hWndLostFocus);
  392.     void         EvKillFocus(HWND hWndGetFocus);
  393.     void         EvChildInvalid(HWND);
  394.  
  395.   private:
  396.     // Hidden to prevent accidental copying or assignment
  397.     //
  398.     TRichEdit(const TRichEdit&);
  399.     TRichEdit& operator =(const TRichEdit&);
  400.  
  401.     // The following are TEdit methods which encapsulates messages/API
  402.     // which are *NOT* supported by the RICHEDIT class.
  403.     //
  404.     void    FormatLines(bool addEOL);
  405.     char far* LockBuffer(uint newSize = 0);
  406.     void    UnlockBuffer(const char far*, bool = false);
  407.     void    SetTabStops(int numTabs, const int far*);
  408.     HLOCAL  GetHandle() const;
  409.     void    SetHandle(HLOCAL localMem);
  410.     uint    GetPasswordChar() const;
  411.     void    SetPasswordChar(uint ch);
  412.     void    SetRectNP(const TRect& frmtRect);
  413.  
  414.     // Validators are not applicable to RichEdit where the text
  415.     // is not plain ASCII...
  416.     //
  417.     bool    IsValid(bool reportErr = false);
  418.     void    SetValidator(TValidator* validator);
  419.     void    ValidatorError();
  420.  
  421.   DECLARE_RESPONSE_TABLE(TRichEdit);
  422. };
  423.  
  424. // Generic definitions/compiler options (eg. alignment) following the 
  425. // definition of classes
  426. #include <services/posclass.h>
  427.  
  428. #if defined(BI_NAMESPACE)
  429. } // namespace OWL
  430. #endif
  431.  
  432. //----------------------------------------------------------------------------
  433. // Inline implementation
  434. //
  435.  
  436. //
  437. // Handler of the CM_EDITCUT command - Invokes the 'Cut' method.
  438. //
  439. inline void TRichEdit::CmEditCut()
  440. {
  441.   Cut();
  442. }
  443.  
  444. //
  445. // Handler of the CM_EDITCOPY command - invokes the'Copy' method
  446. //
  447. inline void TRichEdit::CmEditCopy()
  448. {
  449.   Copy();
  450. }
  451.  
  452. //
  453. // Handler of the CM_EDITPASTE command - invokes the 'Paste' method
  454. //
  455. inline void TRichEdit::CmEditPaste()
  456. {
  457.   Paste();
  458. }
  459.  
  460. //
  461. // Handler of the CM_EDITDELETE command - invokes the 'DeleteSelection' 
  462. // method
  463. //
  464. inline void TRichEdit::CmEditDelete()
  465. {
  466.   DeleteSelection();
  467. }
  468.  
  469. //
  470. // Handler of the CM_EDITCLEAR command - invokes the 'Clear' method
  471. //
  472. inline void TRichEdit::CmEditClear()
  473. {
  474.   Clear();
  475. }
  476.  
  477. //
  478. // Handler of the CM_EDITUNDO command - invokes the 'Undo' method
  479. //
  480. inline void TRichEdit::CmEditUndo()
  481. {
  482.   Undo();
  483. }
  484.  
  485. //
  486. // Constructor of a 'TCharFormat' structure initialized with the specified
  487. // mask. NOTE: This constructor is typically used for constructing an
  488. // object which will be filled with the appropriate character format
  489. // information by a rich edit control. The 'mask' specifies which field
  490. // should be filled.
  491. //
  492. inline TCharFormat::TCharFormat(ulong mask)
  493. {
  494.   cbSize = sizeof(CHARFORMAT);
  495.   dwMask = mask;
  496. }
  497.  
  498. //
  499. // Toggle the effect bits specified in the 'flag' parameter
  500. //
  501. inline void TCharFormat::ToggleEffectsBit(ulong flag)
  502. {
  503.   dwEffects ^= flag;
  504. }
  505.  
  506. //
  507. // Toggle the mask bits specified in the 'flag' parameter
  508. //
  509. inline void TCharFormat::ToggleMaskBit(ulong flag)
  510. {
  511.   dwMask ^= flag;
  512. }
  513.  
  514. //
  515. // Construct a TCharRange structure initialized with the specified 'min' and
  516. // 'max' parameters.
  517. //
  518. inline TCharRange::TCharRange(long min, long max)
  519. {
  520.   cpMin = min;
  521.   cpMax = max;
  522. }
  523.  
  524. //
  525. // Construct a default 'TEditStream' object. The members of the base
  526. // 'EDITSTREAM' object are initialized to zero.
  527. //
  528. inline TEditStream::TEditStream()
  529. {
  530.   dwCookie = 0;
  531.   dwError  = 0;
  532.   pfnCallback = 0;
  533. }
  534.  
  535. //
  536. // Construct a 'TEditStream' object initializing the members with the
  537. // specified parameter.
  538. //
  539. inline TEditStream::TEditStream(ulong cookie, EDITSTREAMCALLBACK callback)
  540. {
  541.   dwCookie = cookie;
  542.   dwError  = 0;
  543.   pfnCallback = callback;
  544. }
  545.  
  546. //
  547. // Construct a 'TFindText' describing the text to search for and the range
  548. // of the search using the 'text' and 'range' parameters respectively.
  549. //
  550. inline TFindText::TFindText(const TCharRange& range, const char far* text)
  551. {
  552.   chrg = range;
  553.   lpstrText = CONST_CAST(char far*, text);
  554. }
  555.  
  556. //
  557. // Construct a 'TFindText' describing the text to search for via the 'text'
  558. // parameter, and the range to search for via the 'beg' and 'end' parameters
  559. // respectively.
  560. //
  561. inline TFindText::TFindText(long beg, long end, const char far* text)
  562. {
  563.   chrg.cpMin = beg;
  564.   chrg.cpMax = end;
  565.   lpstrText = CONST_CAST(char far*, text);
  566. }
  567.  
  568. //
  569. // Construct a default 'TFormatRange' object with all members initialized
  570. // to zero.
  571. //
  572. inline TFormatRange::TFormatRange()
  573. {
  574.   memset((FORMATRANGE*)this, 0, sizeof(FORMATRANGE));
  575. }
  576.  
  577. //
  578. // Construct a default 'TTextRange' structure with the data members 
  579. // describing the range and text initialized to zero.
  580. //
  581. inline TTextRange::TTextRange()
  582. {
  583.   chrg.cpMin = 0;
  584.   chrg.cpMax = 0;
  585.   lpstrText  = 0;
  586. }
  587.  
  588. //
  589. // Construct a 'TTextRange' structure by initializing the range and text
  590. // members with the 'rng' and 'buffer' parameters respectively.
  591. //
  592. inline TTextRange::TTextRange(const TCharRange& rng, char far* buffer)
  593. {
  594.   chrg = rng;
  595.   lpstrText = buffer;
  596. }
  597.  
  598. //
  599. // Construct a 'TTextRange' structure by initializing the text with the 
  600. // 'buffer' parameter, and the range using 'beg' and 'end'.
  601. //
  602. inline TTextRange::TTextRange(long beg, long end, char far* buffer)
  603. {
  604.   chrg.cpMin = beg;
  605.   chrg.cpMax = end;
  606.   lpstrText = buffer;
  607. }
  608.  
  609. //
  610. //
  611. //
  612. inline uint    
  613. TRichEdit::GetFormat() const {
  614.   return Format;
  615. }
  616.  
  617. //
  618. //
  619. //
  620. inline void    
  621. TRichEdit::SetFormat(uint fmt) {
  622.   Format = fmt;
  623. }
  624.  
  625. //
  626. //
  627. //
  628. inline bool    
  629. TRichEdit::IsRTF() const {
  630.   return Format == SF_RTF;
  631. }
  632.  
  633. #endif  // OWL_RICHEDIT_H
  634.